package com.qf.service.impl;

import com.qf.mapper.ReaderInfoMapper;
import com.qf.pojo.ReaderInfo;
import com.qf.service.ReaderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ReaderInfoServiceImpl implements ReaderInfoService {

    @Autowired
    private ReaderInfoMapper readerInfoMapper;
    @Override
    public List<ReaderInfo> findAll() {
        return readerInfoMapper.findAll();
    }

    /**
     * 删除用户
     * @param readerId
     */
    @Override
    public void delete(Long readerId) {
        readerInfoMapper.delete(readerId);
    }

    /**
     * 增加用户
     * @param readerInfo
     */
    @Override
    public void addReader(ReaderInfo readerInfo) {
        readerInfoMapper.add(readerInfo);
    }

    /**
     * 通过id查询用户
     * @param readerId
     * @return
     */
    @Override
    public ReaderInfo findById(Long readerId) {
        return readerInfoMapper.findById(readerId);
    }

    /**
     * 修改用户信息
     * @param readerInfo
     */
    @Override
    public void updateReader(ReaderInfo readerInfo) {
        readerInfoMapper.updateReader(readerInfo);
    }
    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public ReaderInfo login(String username, String password) {

        ReaderInfo readerInfo = readerInfoMapper.findByUsername(username);
        if (readerInfo != null){
            if (readerInfo.getPassword().equals(password)) {
                return readerInfo;
            }else {
                throw new RuntimeException("账号或密码错误,请重新输入");

            }
        }else {
            throw new RuntimeException("账号不存在,请重新输入");
        }

    }

    /**
     * 修改密码
     * @param readerInfo
     */
    @Override
    public void updatePassword(ReaderInfo readerInfo) {
        readerInfoMapper.updatePassword(readerInfo);
    }
}
